﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DotNetSpeech;
using System.Media;

namespace Common
{
    public static class PlayVoice
    {

        public static SpVoice voice = null;
        public static SoundPlayer sp = new SoundPlayer();
        public static double playVoice(string str)
        {
            if (str == null)
                return 5;
            if (str.EndsWith(".mp3"))
            {
                //return 5;
                //string mypath = "";
                //if (File.Exists(str))
                //    mypath = str;
                //else if (File.Exists(Application.StartupPath + "\\" + str))
                //    mypath = Application.StartupPath + "\\" + str;
                //{
                //    clsMCI cm = new clsMCI();
                //    cm.FileName = mypath;
                //    cm.play();
                //    return cm.Duration;
                //}
            }
            else
            {
                // SpeechVoiceSpeakFlags SpFlags = SpeechVoiceSpeakFlags.SVSFlagsAsync;
                try
                {
                    if (voice == null)
                        voice = new SpVoice();
                    if ((int)voice.Status.RunningState == 2)
                    {
                        voice.Speak(string.Empty, SpeechVoiceSpeakFlags.SVSFPurgeBeforeSpeak);
                        voice.Speak(str, SpeechVoiceSpeakFlags.SVSFlagsAsync);
                    }
                    else
                    {
                        voice.Speak(str, SpeechVoiceSpeakFlags.SVSFlagsAsync);
                    }
                }
                catch (Exception ex)
                {
                }
            }

            return 0;
        }

    }
}
